/*
 * InterfaceGenerator.java
 *
 * Created on 08-Jul-2009, 13:35:22
 * Copyright newtonr6
 */
package iffmyexp;

import javax.faces.FacesException;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlInputTextarea;
import com.icesoft.faces.component.ext.HtmlSelectBooleanCheckbox;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.HtmlSelectOneRadio;
import java.io.*;
import java.util.Iterator;
import java.util.Vector;
import javax.faces.event.ValueChangeEvent;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class InterfaceGenerator {

    public static Vector inputsVector;
    private static Vector valuesVector;
    private static int addedCount;
    static int selectedInputIndex;
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    public static HtmlDataTable inputsTable = new HtmlDataTable();

    public HtmlDataTable getInputsTable() {
        return inputsTable;
    }

    public void setInputsTable(HtmlDataTable hdt) {
        this.inputsTable = hdt;
    }
    public String selectedInput = null;

    public String getSelectedInput() {
        return selectedInput;
    }

    public void setSelectedInput(String str) {
        this.selectedInput = str;
    }
    public static HtmlInputText nameTextarea = new HtmlInputText();

    public HtmlInputText getNameTextarea() {
        return nameTextarea;
    }

    public void setNameTextarea(HtmlInputText ita) {
        this.nameTextarea = ita;
    }
    public static String savedMessage = "Description not saved";

    public String getSavedMessage() {
        return savedMessage;
    }

    public void setSavedMessage(String str) {
        savedMessage = str;
    }

    public static String savedMessageStyle = "color:red; font-weight:bold";

    public String getSavedMessageStyle() {
        return savedMessageStyle;
    }

    public void setSavedMessageStyle(String str) {
        savedMessageStyle = str;
    }

    public static HtmlInputText optionInputText = new HtmlInputText();

    public HtmlInputText getOptionInputText() {
        return optionInputText;
    }

    public void setOptionInputText(HtmlInputText it) {
        this.optionInputText = it;
    }
    public static HtmlInputTextarea exampleTextarea = new HtmlInputTextarea();

    public HtmlInputTextarea getExampleTextarea() {
        return exampleTextarea;
    }

    public void setExampleTextarea(HtmlInputTextarea ita) {
        this.exampleTextarea = ita;
    }
    public static HtmlInputTextarea descriptionTextarea = new HtmlInputTextarea();

    public HtmlInputTextarea getDescriptionTextarea() {
        return descriptionTextarea;
    }

    public void setDescriptionTextarea(HtmlInputTextarea ita) {
        this.descriptionTextarea = ita;
    }
    public static HtmlSelectOneMenu uicMenu = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getuicMenu() {
        return uicMenu;
    }

    public void setuicMenu(HtmlSelectOneMenu sol) {
        this.uicMenu = sol;
    }
    public String addedOption = new String();

    public String getAddedOption() {
        return addedOption;
    }

    public void setAddedOption(String str) {
        this.addedOption = str;
    }
    public static HtmlSelectOneMenu dataTypesMenu = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getDataTypesMenuMenu() {
        return dataTypesMenu;
    }

    public void setDataTypesMenuMenu(HtmlSelectOneMenu sol) {
        this.dataTypesMenu = sol;
    }
    public static HtmlOutputText submitStatusText = new HtmlOutputText();

    public HtmlOutputText getSubmitStatusText() {
        return submitStatusText;
    }

    public void setSubmitStatusText(HtmlOutputText hot) {
        this.submitStatusText = hot;
    }
    public static HtmlInputText minInputText = new HtmlInputText();

    public HtmlInputText getMinInputText() {
        return minInputText;
    }

    public void setMinInputText(HtmlInputText it) {
        this.minInputText = it;
    }
    public static HtmlInputText maxInputText = new HtmlInputText();

    public HtmlInputText getMaxInputText() {
        return maxInputText;
    }

    public void setMaxInputText(HtmlInputText it) {
        this.maxInputText = it;
    }
    public static HtmlInputText lengthInputText = new HtmlInputText();

    public HtmlInputText getLengthInputText() {
        return lengthInputText;
    }

    public void setLengthInputText(HtmlInputText it) {
        this.lengthInputText = it;
    }
    public HtmlSelectOneRadio typeRadio = new HtmlSelectOneRadio();

    public HtmlSelectOneRadio getTypeRadio() {
        return typeRadio;
    }

    public void setTypeRadio(HtmlSelectOneRadio sor) {
        this.typeRadio = sor;
    }
    public static boolean editNameVis = false;

    public boolean getEditNameVis() {
        return editNameVis;
    }

    public void setEditNameVis(boolean rv) {
        this.editNameVis = rv;
    }
    public static boolean editName1Vis = false;

    public boolean getEditName1Vis() {
        return editName1Vis;
    }

    public void setEditName1Vis(boolean rv) {
        this.editName1Vis = rv;
    }
    public static boolean compsVis = false;

    public boolean getcompsVis() {
        return compsVis;
    }

    public void setcompsVis(boolean cv) {
        this.compsVis = cv;
    }
    public boolean popupVis = false;

    public boolean getPopupVis() {
        return popupVis;
    }

    public void setPopupVis(boolean pv) {
        this.popupVis = pv;
    }
    public static boolean confirmVis = false;

    public boolean getConfirmVis() {
        return confirmVis;
    }

    public static void setConfirmVis(boolean cv) {
        InterfaceGenerator.confirmVis = cv;
    }
    public static boolean listInputVis = false;

    public boolean getListInputVis() {
        return listInputVis;
    }

    public static void setListInputVis(boolean cv) {
        InterfaceGenerator.listInputVis = cv;
    }
    public static boolean descVis = false;

    public boolean getDescVis() {
        return descVis;
    }

    public static void setDescVis(boolean cv) {
        descVis = cv;
    }
    public static boolean restrictVis = false;

    public boolean getRestrictVis() {
        return restrictVis;
    }

    public void setRestrictVis(boolean rv) {
        this.restrictVis = rv;
    }
    public static boolean stringVis = true;

    public boolean getStringVis() {
        return stringVis;
    }

    public void setStringVis(boolean rv) {
        this.stringVis = rv;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public InterfaceGenerator() {
    }

    public String editName() {
        editNameVis = false;
        editName1Vis = true;
        return null;
    }

    public String confirmEditName() {
        try {
            String newName = (String) nameTextarea.getText();
            System.out.println(newName);
            PushWorkflow.setWorkflow(newName);
            editNameVis = true;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public String toggleDesc() {
        savedMessage = "Description not saved";
        savedMessageStyle = "color:red; font-weight:bold";
        descVis = !descVis;
        return null;
    }

    public String addComponent() {

        valuesVector = new Vector();

        popupVis = true;
        selectedInputIndex = inputsTable.getRowIndex();
        selectedInput = PushWorkflow.inputs[selectedInputIndex].name;

        exampleTextarea.setValue("example input");
        descriptionTextarea.setValue("input description");

        return null;
    }

    public String cancelAddComponent() {
        valuesVector.clear();
        popupVis = false;
        return null;
    }

    public String addOption() {

        valuesVector.add(optionInputText.getValue());
        addedOption = (String) optionInputText.getValue() + " added to list";
        optionInputText.setValue("");
        return null;
    }

    public String confirmAddComponent() {

        inputModel[] inputs = PushWorkflow.inputs;
        inputModel input = inputs[selectedInputIndex];

        String name = input.name;
        String UIComponent = (String) uicMenu.getValue();
        String dataType = (String) dataTypesMenu.getValue();
        String min = (String) minInputText.getValue();
        String max = (String) maxInputText.getValue();
        String length = (String) lengthInputText.getValue();
        String example = (String) exampleTextarea.getValue();
        String description = (String) descriptionTextarea.getValue();

        inputModel inputOut = new inputModel(name, UIComponent, valuesVector, dataType, min, max, length, example, description, "", "", true);

        inputsVector.add(inputOut);

        PushWorkflow.count = PushWorkflow.count + 1;
        if (PushWorkflow.count == inputs.length) {
            confirmVis = true;
        }

        /*System.out.println(name + ", " + UIComponent + ", " + dataType + ", " + example);
        Iterator itre = valuesVector.iterator();
        while (itre.hasNext()) {
        System.out.println((String) itre.next());
        }*/

        Iterator itrb = inputsVector.iterator();
        while (itrb.hasNext()) {
            inputModel im = (inputModel) (itrb.next());
            System.out.println(im.name);
            Iterator itrc = im.options.iterator();
            while (itrc.hasNext()) {
                System.out.println((String) itrc.next());
            }
        }

        input.message = "Component defined";
        input.icon = "/resources/icons/png/16x16/Yes.png";
        input.complete = true;

        exampleTextarea.setValue("example input");
        descriptionTextarea.setValue("input description");
        optionInputText.setValue("");
        addedOption = "";
        popupVis = false;

        return null;
    }

    public String writeXML() {

        String description = PushWorkflow.desc;
        if (description.equals("")) {
            description = "No description available";
        }
        String tmpDesc = description.replaceAll("<", "&lt;");
        String tmpDesc2 = tmpDesc.replaceAll(">", "&gt;");
        description = tmpDesc2;

        //description.replace(">", "&gt;");

        try {
            // Create file
            boolean exists = (new File("wf_data/" + PushWorkflow.workflow + ".xml")).exists();
            if (exists != true) {
                File file = new File("wf_data/" + PushWorkflow.workflow + ".xml");
                System.out.print("exists");

                FileWriter fstream = new FileWriter(file, true);
                BufferedWriter out = new BufferedWriter(fstream);

                out.write("<workflow>\n");
                out.write("\t<idonserver>" + PushWorkflow.serverID + "</idonserver>\n");
                out.write("\t<description>" + description + "</description>\n");
                out.write("\t<inputs>\n");
                Iterator itrb = inputsVector.iterator();
                while (itrb.hasNext()) {

                    inputModel im = (inputModel) (itrb.next());

                    out.write("\t\t<input>\n\t\t\t" +
                            "<name>" + im.name + "</name>\n\t\t\t" +
                            "<type>" + im.UIComponent + "</type>\n\t\t\t" +
                            "<values>");

                    Iterator itr = im.options.iterator();
                    while (itr.hasNext()) {
                        out.write("\n\t\t\t\t<value>" + (String) itr.next() + "</value>");
                    }



                    if (im.example.equals("")) {
                        im.example = "no example input given";
                    }

                    if (im.description.equals("")) {
                        im.description = "no description given";
                    }


                    out.write("\n\t\t\t</values>\n\t\t\t" +
                            /*"<restriction>\n\t\t\t\t"+
                            "<min>"+im.min+"</min>\n\t\t\t\t" +
                            "<max>"+im.max+"</max>\n\t\t\t\t" +
                            "<length>"+im.length+"</length>\n\t\t\t" +
                            "</restriction>\n\t\t\t" +*/
                            "<example>" + im.example + "</example>\n\t\t\t" +
                            "<description>" + im.description + "</description>\n\t\t" +
                            "</input>\n");
                }

                out.write("\t</inputs>\n");
                out.write("</workflow>");

                out.close();

                confirmVis = false;

                submitStatusText.setStyle("color:green; font-size:12px; font-weight:bold");
                submitStatusText.setValue("Interface schema saved");
            } else {
                submitStatusText.setStyle("color:red; font-size:12px; font-weight:bold");
                submitStatusText.setValue("An interface schema for a workflow with this name has already been defined");
            }

        } catch (Exception ex) {//Catch exception if any
            System.out.println("Error: " + ex.getMessage());
        }


        return null;
    }

    public void componentChanged(ValueChangeEvent event) {

        String UIComponent = (String) uicMenu.getValue();
        if (UIComponent.equals("dropdown") || UIComponent.equals("checkbox") || UIComponent.equals("radio")) {
            listInputVis = true;
        } else {
            listInputVis = false;
        }
    }

    public void restrictionChanged(ValueChangeEvent vce) {

        restrictVis = !restrictVis;
    }

    public void typeChanged(ValueChangeEvent event) {

        String dataType = (String) typeRadio.getValue();
        if (dataType.equals("String")) {
            stringVis = true;
        } else {
            stringVis = false;
        }
    }

    public void saveDescription(ValueChangeEvent event) {

        savedMessage = "Description saved";
        savedMessageStyle="color:green; font-weight:bold";
    }
}

